package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.db.domain.MarketCommentExample;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.db.mapper.MarketCommentMapper;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.CommentListVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WxCommentServiceImpl implements WxCommentService {

    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Autowired
    MarketUserMapper marketUserMapper;

    @Override
    public List<Object> list(Integer valueId, Byte type, Integer limit, Integer page, Integer showType) {

        List<Object> list = new ArrayList<>();


        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        criteria.andLogicalDeleted(true);
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(type);
        if(showType.equals(1)) {
            criteria.andHasPictureEqualTo(true);
        }
        PageHelper.startPage(page, limit);
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);

        for (MarketComment marketComment : marketComments) {
            CommentListVo commentListVo = new CommentListVo();
            MarketUser marketUser = marketUserMapper.selectByPrimaryKey(marketComment.getUserId());

            Map<String, Object> userInfo = new HashMap<>();
            userInfo.put("nickName", marketUser.getUsername());
            userInfo.put("avaturUrl", marketUser.getAvatar());
            commentListVo.setUserInfo(userInfo);
            commentListVo.setAddTime(marketComment.getAddTime());
            commentListVo.setPicList(marketComment.getPicUrls());
            if (marketComment.getAdminContent() != null) {                commentListVo.setAdminContent(marketComment.getAdminContent());
                commentListVo.setAdminContent(marketComment.getAdminContent());
            }else{
                commentListVo.setAdminContent("");
            }
            commentListVo.setContent(marketComment.getContent());
            list.add(commentListVo);
        }
        return list;
    }

    @Override
    public Map<String, Object> count(Integer valueId, Byte type) {

        Map<String, Object> data = new HashMap<>();

        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        criteria.andLogicalDeleted(true);

        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(type);
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);
        data.put("allCount", marketComments.size());

        criteria.andHasPictureEqualTo(true);
        List<MarketComment> marketCommentsHasPic = marketCommentMapper.selectByExample(marketCommentExample);
        data.put("hasPicCount", marketCommentsHasPic.size());

        return data;
    }

    @Override
    public MarketComment post(MarketComment comment) {

        MarketUser user = (MarketUser) SecurityUtils.getSubject().getSession().getAttribute("user");
        comment.setUserId(user.getId());
        comment.setAddTime(LocalDateTime.now());
        comment.setUpdateTime(LocalDateTime.now());
        comment.setDeleted(false);

        marketCommentMapper.insert(comment);
        return comment;
    }
}
